﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MovieBuddy.Dialogs;
using MovieBuddy.Helpers;
using MovieBuddy.Properties;

namespace MovieBuddy.Controllers
{
    /// <summary>
    /// Create a new controller every time
    /// </summary>
    public class OptionsDialogController
    {
        private readonly MovieDetector detector;
        private readonly OptionsDialog dialog;
        private string originalDataFolder;

        public OptionsDialogController(MovieDetector detector)
        {
            this.detector = detector;
            dialog = new OptionsDialog(this);
        }

        public void Execute()
        {
            originalDataFolder = Settings.Default.DataFolder;
            dialog.MovieDetectionFolder = Settings.Default.NewMovieFinderFolder;
            dialog.OverrideDataDirectory = originalDataFolder;

            if (dialog.ShowDialog() == true)
            {
                Settings.Default.NewMovieFinderFolder = dialog.MovieDetectionFolder;
                Settings.Default.DataFolder = dialog.OverrideDataDirectory;
                Settings.Default.Save();

                if (dialog.OverrideDataDirectory != originalDataFolder)
                {
                    Application.Current.Shutdown();
                }

                detector.DetectVideoFolder = dialog.MovieDetectionFolder;
            }
        }

        public bool VerifyValues()
        {
            if (dialog.OverrideDataDirectory == originalDataFolder) return true;

            var result = MessageBox.Show(dialog, "Updating the data folder will force MovieBuddy to close.", "Update data folder",
                    MessageBoxButton.OKCancel);
            return result == MessageBoxResult.OK;
        }
    }
}
